package com.danger.Model.action;

import android.content.Context;
import android.text.TextUtils;
import com.danger.Model.bean.UserInfoBean;
import com.danger.Model.listener.OnLoginListener;
import com.danger.Util.CheckCode;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

/**
 * Created by danger on 2016/3/17.
 */
public class LoginModelImpl implements LoginModel {

    private Context context;
    private String USERNAME_IS_NULL = "用户名为空";
    private String PASSWORD_IS_NULL = "密码为空";
    private String IS_RIGHT = "正确";

    public LoginModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void login(String username,String password, final OnLoginListener listener) {
        BmobUser.loginByAccount(context, username, password, new LogInListener<UserInfoBean>() {
            @Override
            public void done(UserInfoBean userInfo, BmobException e) {
                if (e == null) {
                    listener.onSuccess(userInfo);
                } else {
                    listener.onError(CheckCode.checkByCode(e.getErrorCode()));
                }
            }
        });
    }

    @Override
    public String check(String username,String password) {
        if (TextUtils.isEmpty(username)) {
            return USERNAME_IS_NULL;
        }
        if (TextUtils.isEmpty(password.trim())) {
            return PASSWORD_IS_NULL;
        }
        return IS_RIGHT;
    }
}
